package org.jlamp.core.runtime;

import org.jlamp.core.prop.JlampFileProp;
import org.jlamp.core.util.FileUtils;
import org.jlamp.core.util.JlampUtils;

import java.io.File;
import java.io.IOException;

/**
 * 运行时数据
 *
 * @author zhangcs
 */
public class JlampData {

    /**
     * 数据分隔符
     */
    private final String separator = ",";

    /**
     * 进程号
     */
    private final String pid;

    /**
     * 版本号
     */
    private String version = "1.0";

    /**
     * 端口
     */
    private String port = null;

    /**
     * 状态（0：停止，1：运行）
     */
    private String status = "1";

    private JlampData(String data, String pid) {
        this.pid = pid;
        if (data == null || data.length() <= 0) {
            return;
        }
        String[] array = data.split(separator);
        int len = array.length;
        if (len > 0) {
            this.version = array[0];
        }
        if (len > 1) {
            this.port = array[1];
        }
        if (len > 2) {
            this.status = array[2];
        }
    }

    @Override
    public String toString() {
        return version + separator + port + separator + status;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    /**
     * 设置端口
     *
     * @param port 端口号
     */
    public void port(String port) {
        this.port = port;
    }

    /**
     * 获取端口
     *
     * @return 返回端口号
     */
    public String port() {
        return port;
    }

    /**
     * 修改状态为停止
     *
     * @throws IOException
     */
    public void stop() throws IOException {
        this.status = "0";
        flush();
    }

    public String status() {
        return status;
    }

    /**
     * 写文件
     *
     * @throws IOException 操作失败时抛出异常
     */
    public synchronized void flush() throws IOException {
        FileUtils.write(this.toString(), file(this.pid));
    }

    /**
     * 获取指定进程的数据文件
     *
     * @param pid 进程号
     * @return 返回数据对象
     * @throws IOException 打开文件失败时抛出异常
     */
    public static JlampData get(String pid) throws IOException {
        String content = FileUtils.read(file(pid));
        return new JlampData(content, pid);
    }

    /**
     * 获取当前进程的数据文件
     *
     * @return 返回数据对象
     * @throws IOException 打开文件失败时抛出异常
     */
    public static JlampData get() throws IOException {
        return get(null);
    }

    private static File file(String pid) throws IOException {
        String path = path(pid);
        File file = new File(path);
        if (!file.exists()) {
            // 不存在时创建文件
            file.createNewFile();
        }
        return file;
    }

    public static String path(String pid) {
        String home;
        if (pid == null || pid.length() <= 0) {
            home = JlampUtils.home();
        } else {
            home = JlampUtils.home(pid);
        }
        return home + File.separator + JlampFileProp.JLAMP_DATA_FILE_NAME;
    }

    public void start(String port) throws IOException {
        this.status = "1";
        this.port = port;
        flush();
    }

}
